package top.fengyulou.config.interceptor;

import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import top.fengyulou.code.service.ProjectService;
import top.fengyulou.config.SysConfig;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 选择项目拦截
 */
@Component
public class ProjectInterceptor implements HandlerInterceptor {
    /**
     * 选择项目
     */
    public static final String SELECT_PROJECT = "sp";

    @Resource
    private SysConfig sysConfig;
    @Resource
    private ProjectService projectService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (ObjectUtils.isEmpty(projectService.getSessionProject(request.getSession()))) {
            response.sendRedirect(sysConfig.getDomainName() + "/v2/u/select/project");
            return false;
        }
        return true;
    }
}
